package aes.general;

import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import aes.client.AesClient;
import aes.gui.AdminFrame;
import aes.gui.StudentFrame;
import aes.gui.TeacherFrame;


/**
 * The Class GuiControlBeta. Final Class Holds Methods and Variables responsible for initial login to system.
 */
final public class GuiControlBeta {
	
	
	public static String userid;
	private static String pass;
	private static AesClient client;

	
	/**
	 * Check user request to server.Request checks if Userid exists in DB and password is correct
	 *
	 * @param userid the userid
	 * @param password the password
	 * @param client the client
	 */
	public static void CheckUserRequestToServer(String userid, String password,
			AesClient client) {
		GuiControlBeta.userid = userid;
		GuiControlBeta.pass = password;
		GuiControlBeta.client = client;

		String query = new String("select * from User WHERE ID=" + userid);
		Msg request1 = new Msg(query, "100");
		client.handleMessageFromClientUI(request1);
	}

	
	/**
	 * Process login query.Called then comes response from server for CheckUserRequestToServer call.
	 * If UserdiD and Password correct opens appropriate window , else presents error Message 
	 *
	 * @param msg the Message from server
	 * @return the int , always returns 0
	 */
	@SuppressWarnings("unchecked")
	public static int ProcessLoginQuery(Object msg) {
		ArrayList<String> arr = new ArrayList<String>();
		arr = (ArrayList<String>) ((Msg) msg).getMsg();

		if (arr.get(0).equals("error")) {
			JOptionPane.showMessageDialog(null, "ERROR , Wrong UserID .",
					"Error", JOptionPane.ERROR_MESSAGE);
			try {
				getClient().closeConnection();
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		} else if (arr.get(0).equals(userid) && arr.get(3).equals(pass)) {
			JOptionPane.showMessageDialog(null, "USERID and pass ok  ", "",
					JOptionPane.INFORMATION_MESSAGE);

			if (arr.get(6).equals("t")) {
				ArrayList<String> sid = new ArrayList<String>();
				ArrayList<String> sname = new ArrayList<String>();
				for (int i = 7; i < arr.size(); i++) {
					sid.add(arr.get(i));
					sname.add(arr.get(++i));
				}
				
				Teacher teacher = new Teacher(userid, arr.get(1), arr.get(2),
						pass, arr.get(4), arr.get(5),sid,sname);
				System.out.println("teacher");
				TeacherFrame tf = new TeacherFrame(teacher);
				tf.setVisible(true);
			}

			else if (arr.get(6).equals("s")) {
				StudentFrame stud = new StudentFrame();
				stud.setVisible(true);
			}

			else if (arr.get(6).equals("a")) {
				AdminFrame admin = new AdminFrame();
				admin.setVisible(true);
			}
		}

		else {
			JOptionPane.showMessageDialog(null, "Wrong password  ", "",
					JOptionPane.ERROR_MESSAGE);

			try {
				getClient().closeConnection();
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}

		return 0;
	}

	
	/**
	 * Gets the client. Gets the current OCSF client logged in
	 *
	 * @return the client
	 */
	public static AesClient getClient() {
		return client;
	}

	/**
	 * Sets the client. Sets the current OCSF client logged in
	 *
	 * @param client the new client
	 */
	public static void setClient(AesClient client) {
		GuiControlBeta.client = client;
	}
	

}
